package com.xindaoapp.happypet.fragments;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.xindao.photonics.fragments.manager.BaseFragment;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.utils.IRequest;
import com.xindaoapp.happypet.utils.KeyValuePair;

/**
 * 找回密码
 * 
 * @author chenghao
 * 
 */
public class ForgetPasswordFragment extends BaseFragment implements OnClickListener {
    private EditText vEmail;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_forget_password, container, false);
    }

    @Override
    protected void initViews(View view) {
        super.initViews(view);
        vEmail = (EditText) view.findViewById(R.id.email);
    }

    @Override
    protected void initEvents(View view) {
        super.initEvents(view);
        view.findViewById(R.id.confirm).setOnClickListener(this);
    }

    @Override
    protected String getTitleName() {
        return "找回密码";
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.confirm:
                if (checkInput()) {
                    getMoccaApi().getPassword(vEmail.getText().toString().trim(),
                            new IRequest<KeyValuePair<Integer, String>>() {

                                @Override
                                public void request(KeyValuePair<Integer, String> t) {
                                    if (t != null) {
                                        if (t.first == 0) {
                                            backFragment();
                                            Toast.makeText(mActivity, "", Toast.LENGTH_SHORT)
                                                    .show();
                                        } else if (t.first == 2) {
                                            Toast.makeText(mActivity, "邮箱地址不存在！",
                                                    Toast.LENGTH_SHORT).show();
                                        }
                                    } else {
                                        showNetErrorToast();
                                    }
                                }
                            });
                }
                break;

            default:
                break;
        }
    }

    private boolean checkInput() {
        if (TextUtils.isEmpty(vEmail.getText().toString().trim())) {
            Toast.makeText(mActivity, "请输入邮箱地址！", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (!vEmail
                .getText()
                .toString()
                .trim()
                .matches(
                        "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$")) {
            Toast.makeText(mActivity, "邮箱格式不正确", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

}
